#!/usr/bin/perl -w
#
#  uncustomize-mapnik-map
#  attempts to produce a osm-template.xml from the current osm.xml
#

use strict;
$ENV{MAPNIK_PREFIX} ||= "planet_osm";

open(IN, '<', 'osm.xml');

while(<IN>) {
    s/$ENV{MAPNIK_SYMBOLS_DIR}/%SYMBOLS_DIR%/;
    s/$ENV{MAPNIK_WORLD_BOUNDARIES_DIR}/%WORLD_BOUNDARIES_DIR%/;
    s#<Parameter name="dbname">$ENV{MAPNIK_DBNAME}</Parameter>#<Parameter name="dbname">%DBNAME%</Parameter>#;
    s#$ENV{MAPNIK_PREFIX}#%PREFIX%#;

    if ($ENV{'MAPNIK_DBHOST'} ne '') {
	s#<Parameter name="host">$ENV{MAPNIK_DBHOST}</Parameter>#<Parameter name="host">%DBHOST%</Parameter>#
    } else {
      if ($_ eq "      <Parameter name=\"type\">postgis</Parameter>\n") {
        print;
	$_ = "      <Parameter name=\"host\">%DBHOST%</Parameter>\n";
      }
    }

    if ($ENV{'MAPNIK_DBPORT'} ne '') {
	s#<Parameter name="port">$ENV{MAPNIK_DBPORT}</Parameter>#<Parameter name="port">%DBPORT%</Parameter>#
    } else {
      if ($_ eq "      <Parameter name=\"host\">%DBHOST%</Parameter>\n") {
        print;
	$_ = "      <Parameter name=\"port\">%DBPORT%</Parameter>\n";
      }
    }

    if ($ENV{'MAPNIK_DBUSER'} ne '') {
	s#<Parameter name="user">$ENV{MAPNIK_DBUSER}</Parameter>#<Parameter name="user">%DBUSER%</Parameter>#
    } else {
      if ($_ eq "      <Parameter name=\"port\">%DBPORT%</Parameter>\n") {
        print;
	$_ = "      <Parameter name=\"user\">%DBUSER%</Parameter>\n";
      }
    }

    if ($ENV{'MAPNIK_DBPASS'} ne '') {
	s#<Parameter name="password">$ENV{MAPNIK_DBPASS}</Parameter>#<Parameter name="password">%DBPASS%</Parameter>#
    } else {
      if ($_ eq "      <Parameter name=\"user\">%DBUSER%</Parameter>\n") {
        print;
	$_ = "      <Parameter name=\"password\">%DBPASS%</Parameter>\n";
      }
    }

    print;
}

